﻿<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Main.Master" AutoEventWireup="true"
    CodeBehind="List.aspx.cs" Inherits="APMS.WebSite.Views.Todo.List" %>

<%@ Import Namespace="APMS.WebSite.Common" %>    
<%@ Import Namespace="APMS.WebSite.Models.DataStore" %>
<%@ Import Namespace="APMS.WebSite.Models.ViewData.UserControls" %>

<asp:Content ID="ScriptContent" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content ID="PageTitleContent" ContentPlaceHolderID="PageTitleHolder" runat="server">
    <h2 class="nodesc"><%= Html.Encode(((Project)ViewData[ProjectBaseController.CURRENT_PROJECT]).ProjectName)%></h2>
    <h3>To-do Lists</h3>
</asp:Content>
<asp:Content ID="NavigationMenuContent" ContentPlaceHolderID="NavigationMenuHolder" runat="server">
    <%=Html.RenderUserControl("~/Views/Shared/UserControls/MainNav.ascx", ViewData[BaseController.MAIN_NAV])%>
</asp:Content>
<asp:Content ID="CommandLinkContent" ContentPlaceHolderID="CommandLinkHolder" runat="server">
</asp:Content>
<asp:Content ID="SidebarContent" ContentPlaceHolderID="SidebarHolder" runat="server">
    <div class="widget">
        <h3 class="widgetTitle">Actions</h3>
        <div class="widgetContent">
            <%=Html.Image(AppHelper.GetThemePath() + "images/add.png", new { alt="New", title="New ToDo", style="vertical-align:top" }) %>
            <%=Html.ActionLink<TodoController>(x => x.Create(((Project)ViewData[ProjectBaseController.CURRENT_PROJECT]).ProjectID), "Create a new Todo")%>
        </div>
    </div>
</asp:Content>
<asp:Content ID="MainContent" ContentPlaceHolderID="ContentHolder" runat="server">
    <div class="contentWrap">
        <%
            List<ToDo> allToDo= ViewData[TodoController.ALL_TODOS] as List<ToDo>;
            List<ToDoItem> allToDoItems = ViewData[TodoController.ALL_TODO_ITEMS] as List<ToDoItem>;
            // TODO: query 2 - get all ToDoItem then create the mapping using dictionary or other datastructure

            if ((allToDo != null) && (allToDo.Count > 0))
            {
                StringBuilder builder = null;
                Project currentProject = null;
                List<ToDoItem> localTodoItems = null;
                bool hasToDoItems = false;
                
                foreach (ToDo todo in allToDo)
                {
                    // Find ToDoItem for this ToDo
                    localTodoItems = new List<ToDoItem>();
                    foreach (ToDoItem item in allToDoItems)
                    {
                        if (item.ToDoID == todo.ToDoID)
                        {
                            localTodoItems.Add(item);
                        }
                    }

                    builder = new StringBuilder();
                    currentProject = (Project)ViewData[ProjectBaseController.CURRENT_PROJECT];
                    string viewToDoLink = "/InProject/" + currentProject.ProjectID + "/Todo/View/" + todo.ToDoID;
                    
                    
                    // ToDo Content
                    builder.AppendLine("<div class=\"todo\">");
                    builder.AppendFormat("<h1 class=\"title\"><a href=\"{0}\">{1}</a></h1>",
                        viewToDoLink,
                        todo.ToDoName);
                    builder.Append("<div class=\"content\"><ul>");
                    foreach (ToDoItem item in localTodoItems)
                    {
                        // Loop all ToDoItem
                        builder.AppendFormat("<li class=\"todoItem\"><input id=\"todo_{0}_item{1}\" type=\"checkbox\" /> {2} <a href=\"#\">[Edit]</a></li>", 
                            todo.ToDoID, 
                            item.ToDoItemID, 
                            item.ToDoItemDetail);

                        hasToDoItems = true;
                    }
                    builder.Append("</ul></div>");
                    
                    // ToDo Action
                    builder.AppendFormat("<div class=\"meta\"><span>Created by: {0} on {1}</span> | <a href=\"#\">Add an item</a> | <a href=\"#\">Delete this Todo</a></div>", 
                        todo.UserName, 
                        todo.ModifyDate.ToString(AppHelper.DateTimeFormat));
                    
                    
                    builder.AppendLine("</div>");

                    Response.Flush();
                    Response.Write(builder.ToString());
                }
            }
        %>
    </div>
</asp:Content>
